<?

require_once("mysql.php");

$q = "SELECT 
               deal.id, 
               user.name, 
               deal.item, 
               deal.photo, 
               deal.value, 
               deal.location as online,
               seller.name as seller, 
               seller.reference, 
               UNIX_TIMESTAMP(NOW()) - deal.time as elapsed_time, 
               location.location, 
               location.latitude, 
               location.longitude 

      FROM deal 

      LEFT JOIN seller   ON deal.seller     = seller.id 
      LEFT JOIN location ON seller.location = location.id OR deal.location = location.id
      LEFT JOIN user     ON deal.user       = user.id"; 


$lat = @$_GET['lat'];
$lng = @$_GET['lng'];

if (isset($lat) && isset($lng)) {
    $q = $q." WHERE (latitude BETWEEN ".($lat-1.0)." AND ".($lat+1.0).") AND  (longitude BETWEEN ".($lng-1.0)." AND ".($lng+1.0).")";
}

$q = $q." ORDER BY time DESC";

 
$r = MYSQL_Q($q);

$result = array();

while ($row = mysql_fetch_assoc($r)) {
 

    array_push($result, $row);

    if (count($result) > 40) break;
}

echo json_encode($result, true);

?>
